package server

import (
	v1 "gitee.com/sdynasty/clever-layout/api/helloworld/v1"
	"gitee.com/sdynasty/clever-layout/internal/conf"
	"gitee.com/sdynasty/clever-layout/internal/service"
	"gitee.com/sdynasty/clever/log"
	"gitee.com/sdynasty/clever/middleware/logging"
	"gitee.com/sdynasty/clever/middleware/recovery"
	"gitee.com/sdynasty/clever/middleware/validate"
	"gitee.com/sdynasty/clever/transport/http"
)

// NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server {
	var opts = []http.ServerOption{
		http.Middleware(
			recovery.Recovery(),
			//tracing.Server(),
			logging.Server(logger),
			//metrics.Server(),
			validate.Validator(),
		),
	}
	if c.Http.Network != "" {
		opts = append(opts, http.Network(c.Http.Network))
	}
	if c.Http.Addr != "" {
		opts = append(opts, http.Address(c.Http.Addr))
	}
	if c.Http.Timeout != nil {
		opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
	}
	srv := http.NewServer(opts...)
	v1.RegisterGreeterHTTPServer(srv, greeter)
	return srv
}
